<?php
class imagecontent extends CI_Controller {

	function __construct()
	{
		parent::__construct();
		$this->load->model('modimagecontent');
	}
	
	function index()
	{
		$config['base_url'] = base_url().'imagecontent/index/';
		$config['total_rows'] = $this->db->count_all('images_content');
		$config['per_page'] = 15;
		$config['uri_segment'] = 3;
		
		$config['full_tag_open'] = '<div class="pagination"><ul>';
		$config['full_tag_close'] = '</ul></div><!--pagination-->';

		$config['first_link'] = '&laquo; First';
		$config['first_tag_open'] = '<li class="prev page">';
		$config['first_tag_close'] = '</li>';

		$config['last_link'] = 'Last &raquo;';
		$config['last_tag_open'] = '<li class="next page">';
		$config['last_tag_close'] = '</li>';

		$config['next_link'] = 'Next &rarr;';
		$config['next_tag_open'] = '<li class="next page">';
		$config['next_tag_close'] = '</li>';

		$config['prev_link'] = '&larr; Previous';
		$config['prev_tag_open'] = '<li class="prev page">';
		$config['prev_tag_close'] = '</li>';

		$config['cur_tag_open'] = '<li class="active"><a href="">';
		$config['cur_tag_close'] = '</a></li>';

		$config['num_tag_open'] = '<li class="page">';
		$config['num_tag_close'] = '</li>';
		
		$this->pagination->initialize($config);
		
		$token = $this->session->userdata('token');
		if($token){
			$data['results'] = $this->modimagecontent->getList($config['per_page'],$this->uri->segment(3));
			$this->load->view('imagecontent/list',$data);
		}else{
			redirect('login');
		}
	}
    
	function add(){
		$data['mode'] = "Tambah Content";
		$data['action'] = "save";
		$this->load->view('imagecontent/form',$data);
	}
	
	function edit($id_image){
		$data['mode'] = "Ubah Content";
		$data['action'] = "update";
		$data['detail'] = $this->modimagecontent->detail($id_image);
		$this->load->view('imagecontent/form',$data);
	}
	
	function save()
	{
		$id_image = $this->input->post('id_image');
		$type_image = $this->input->post('type_image');
		$deskripsi_gambar = $this->input->post('deskripsi_gambar');
		
		$config['upload_path'] = './images/uploads/content_images';
		$config['allowed_types'] = 'jpg|png|gif|jpeg';
		
		$this->load->library('upload', $config);
		$this->upload->do_upload('image_content');
		$getFile = $this->upload->data();
		$path_foto = $getFile['file_name'];
		
		if($path_foto != ""){	
			if ($this->upload->display_errors())
			{
				$this->session->set_flashdata('error', true);
				$image_content="";
			}
			else{
				$image_content = $getFile['file_name'];
			}
		}else{
			$image_content = $this->input->post('image_content_tmp');
		}
		
		$submit = $this->input->post('submit');	
		if($submit)
		{
			$this->modimagecontent->setData($id_image,$type_image,$image_content,$deskripsi_gambar);
			$this->modimagecontent->create();
			$this->session->set_flashdata('success', true);
			redirect('imagecontent');
		}
		$this->session->set_flashdata('error', true);
		redirect('imagecontent');
	}
	
	function update()
	{
		$id_image = $this->input->post('id_image');
		$type_image = $this->input->post('type_image');
		$deskripsi_gambar = $this->input->post('deskripsi_gambar');
		
		$config['upload_path'] = './images/uploads/content_images';
		$config['allowed_types'] = 'jpg|png|gif|jpeg';
		
		$this->load->library('upload', $config);
		$this->upload->do_upload('image_content');
		$getFile = $this->upload->data();
		$path_foto = $getFile['file_name'];
		
		if($path_foto != ""){	
			if ($this->upload->display_errors())
			{
				$this->session->set_flashdata('error', true);
				$image_content="";
			}
			else{
				$image_content = $getFile['file_name'];
			}
		}else{
			$image_content = $this->input->post('image_content_tmp');
		}
		
		$submit = $this->input->post('submit');	
		if ($submit)
		{
			$this->modimagecontent->setData($id_image,$type_image,$image_content,$deskripsi_gambar);
			$this->modimagecontent->update($id_image);
			$this->session->set_flashdata('success', true);
			redirect('imagecontent');
		}
		$this->session->set_flashdata('error', true);
		redirect('imagecontent');
	}
	
	function delete($id_image)
	{
		$getHapus = $this->modimagecontent->detail($id_image);
		@unlink("images/uploads/content_images/".$getHapus[0]['image_content']);
		
		if ($this->modimagecontent->remove($id_image)){
			$this->session->set_flashdata('delete', true);
			redirect('imagecontent','refresh');
		}
		$this->session->set_flashdata('error', true);
		redirect('imagecontent');
	}	
	
	function searchData(){
		$name = $this->input->post('name');
		$data['results'] = $this->modimagecontent->getListSearch($name);
		$this->load->view('imagecontent/list',$data);
	}
}
?>